import java.text.DecimalFormat;



public class Individual {

    static int defaultGeneLength = 2;
    static int xMin = -3;
    static int xMax = 3;
    static int yMin = -3;
    static int yMax = 3;
    
    private double[] genes = new double[defaultGeneLength];
    // Cache
    private double fitness = 0;

    // Create a random individual
    public void generateIndividual() {
    	genes[0] = Math.random() * xMax;
    	genes[1] =  Math.random() * yMax;
    }
    public double generateGene(int index){
    	if (index == 0)
    		return Math.random() * xMax;
    	else
    		return Math.random() * yMax;
    }

    /* Getters and setters */
    // Use this if you want to create individuals with different gene lengths
    public static void setDefaultGeneLength(int length) {
        defaultGeneLength = length;
    }
    
    public double getGene(int index) {
        return genes[index];
    }

    public void setGene(int index, double value) {
        genes[index] = value;
        fitness = 0;
    }

    /* Public methods */
    public int size() {
        return genes.length;
    }

    public double getFitness() {
        if (fitness == 0) {
            fitness = FitnessCalc.getFitness(this);
        }
        return fitness;
    }

    @Override
    public String toString() {
        String geneString = "";
        DecimalFormat df = new DecimalFormat("#.##");
        for (int i = 0; i < size(); i++) {
            geneString += df.format(getGene(i)) + " ";
        }
        return geneString;
    }
}
